package com.hashTable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IntersectionofTwoArraysII0130_350 {
    public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map1 = new HashMap<>();
        List<Integer> res = new ArrayList<>();

        for (int i : nums1) {
            map1.put(i,map1.getOrDefault(i,0)+1);
        }
        for (int i : nums2) {
            if (map1.keySet().contains(i) && map1.get(i)>0){
                res.add(i);
                map1.put(i,map1.get(i)-1);
            }
        }
        return res.stream().mapToInt(x->x).toArray();
    }
}
